package main;

import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {


    public static void main(String[] args) {
        int scArray[] = generateAnswer();
        System.out.println("huan请输入4位不重复得数字，例如 5 3 9 0");
        System.out.println("系统" + scArray[0] + "" + scArray[1] + "" + scArray[2] + "" + scArray[3]);
        while (1 == 1) {
            int userArray[] = getPlayerInput();
            boolean yingle = isWin(scArray, userArray);
            if (yingle) {
                Scanner scanner = new Scanner(System.in);
                System.out.println("恭喜您完全猜正确！");
                System.out.println("输入y继续玩游戏,输入n或其他结束游戏!");
                String s = scanner.next();
                if (!"y".equals(s)) {
                    break;
                }
                scArray = generateAnswer();
            } else {
                String alertStr = compareGuessAnswer(scArray, userArray);
                System.out.println(alertStr);
            }

        }
    }






    /**
     * 生成4位数字数组
     * @return
     */
    public static int[] generateAnswer() {

        int array[] = new int[4];

        Random random = new Random();
        int i0 = 10;
        int i1= 10;
        int i2= 10;
        int i3= 10;
        int index = 0;
        while (1 == 1) {
            int num = random.nextInt(10);
            if (num == i0 || num == i1 || num == i2 || num == i3) {
                continue;
            }
            if(index == 0){
                i0 = num;
            }
            if(index == 1){
                i1 = num;
            }
            if(index == 2){
                i2 = num;
            }
            if(index == 3){
                i3 = num;
            }


            array[index] = num;
            index++;
            if(index == 4){
                break;
            }
        }

        return array;
    }



    public static int[] getPlayerInput() {
        int array[] = new int[4];


        while (true) {
            Scanner scanner = new Scanner(System.in);
            String s1 = scanner.nextLine();
            s1 = s1.replace(" ","");
            if ("".equals(s1) || s1.length() != 4) {
                System.out.println("数字长度错误,请继续输入 4位不重复得数字");
                continue;
            }

            String s[] = s1.split("");
            for (int i= 0 ;i<s.length;i++) {
                array[i] = Integer.parseInt(s[i]);
            }
            if(array[0] == array[1] || array[0] == array[2]||array[0] == array[3] || array[2] == array[1] || array[2] == array[3]){
                System.out.println("请输入 4位不重复得数字");
                continue;
            }
            break;
        }
        return array;
    }


    public static boolean isWin(int array1[], int array2[]) {
        if (array1.length != array2.length) {
            return false;
        }
        for (int i = 0; i < array1.length; i++) {
            if (array1[i] != array2[i]) {
                return false;
            }
        }
        return true;
    }



    /**
     *  提示信息
     * @param array1  生成得
     * @param array2   输入得
     * @return
     */
    public static String compareGuessAnswer(int array1[], int array2[]) {

        //值对的
        int count1 = 0;
        //位置对的
        int count2 = 0;

        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array2.length; j++) {
                if(array1[i] == array2[j]){
                    count1 ++;
                }
            }
            if(array1[i] == array2[i]){
                count2++;
            }

        }
        return "猜对了" + count1 + "位数, 其中位置正确得有" + count2 + "位,请继续输入猜测";
    }





}
